home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.01 Jan 90 / MacTutor Help Source / Code Resource Version / C Example / dispatch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-03  |  4.0 KB  |  126 lines  |  [TEXT/KAHL]

  1. /***********************************************************************\
  2. | Purpose :: This module is responsible for channeling menu commands    |
  3. |             to the correct routine.                                    |
  4. |-----------------------------------------------------------------------|
  5. |        Copyright © Joe Pillera, 1990.  All Rights Reserved.            |
  6. \***********************************************************************/
  7.  
  8. #include "demo.h"
  9.  
  10. /***********************************************************************\
  11. |                     void  Do_Command( menuSelection )                    |
  12. |-----------------------------------------------------------------------|
  13. | Purpose :: This routine processes "raw" mouse-down events generated    |
  14. |             by the user, an decides what action should then be taken.    |
  15. |-----------------------------------------------------------------------|
  16. | Arguments ::                                                            |
  17. |     menuSelection -> A longint, containing the menu bar (high byte) and    |
  18. |                     menu item (low byte) of the menu hit event.        |
  19. |-----------------------------------------------------------------------|
  20. | Returns :: void.                                                        |
  21. \***********************************************************************/
  22.  
  23. void Do_Command (menuSelection)
  24. long    menuSelection;
  25. {
  26.     int     menuID, menuItem;    /* The extracted stuff from the longint */
  27.  
  28.     menuID = HiWord (menuSelection);
  29.     menuItem = LoWord (menuSelection);
  30.  
  31.     switch (menuID) {
  32.         case AppleID:
  33.             Do_Apple_Cmd (menuItem);
  34.             break;
  35.         case FileID:
  36.             Do_File_Cmd (menuItem);
  37.             break;
  38.         case EditID:
  39.             break;
  40.     }
  41.     HiliteMenu (0);
  42. }
  43.  
  44.  
  45. /***********************************************************************\
  46. |                     void  Do_Apple_Cmd( menuItem )                        |
  47. |-----------------------------------------------------------------------|
  48. | Purpose :: The user clicked in the Apple menu, so decide whether to    |
  49. |             show the program "About..." box or launch a desk acc.        |
  50. |-----------------------------------------------------------------------|
  51. | Arguments ::                                                            |
  52. |     menuItem -> An integer showing which menu item was selected.        |
  53. |-----------------------------------------------------------------------|
  54. | Returns :: void.                                                        |
  55. \***********************************************************************/
  56.  
  57. static void Do_Apple_Cmd (menuItem)
  58. int menuItem;
  59. {
  60.     DialogPtr     thePtr;        /* Pointer to the about box            */
  61.     GrafPtr        savePort;    /* Good habit:  save old grafport    */
  62.     Str255        DAName;        /* The desk accessory to launch        */
  63.     int            whatHit;    /* Trap for 'ok' and exit            */
  64.     Boolean        done;        /* Exit flag                        */
  65.     
  66.     /* If it's not the about box, open the DA */
  67.     if (menuItem != AboutItem){
  68.         GetItem (GetMHandle (AppleID), menuItem, DAName);
  69.         OpenDeskAcc (DAName);
  70.     }
  71.     else {
  72.         GetPort(&savePort);
  73.         thePtr = GetNewDialog(About_Window, NIL, FRONT);
  74.         Center_Window(thePtr,0,FALSE);
  75.         ShowWindow(thePtr);
  76.         SetPort(thePtr);
  77.         Bold_Button(thePtr,OK_Button);
  78.          ModalDialog(NIL,&whatHit); 
  79.         DisposDialog(thePtr);
  80.         SetPort(savePort);
  81.     }
  82. }
  83.  
  84.  
  85. /***********************************************************************\
  86. |                     void  Do_File_Cmd( menuItem )                        |
  87. |-----------------------------------------------------------------------|
  88. | Purpose :: The user clicked in the File menu, so decide what he chose |
  89. |             and take appropriate action.                                |
  90. |-----------------------------------------------------------------------|
  91. | Arguments ::                                                            |
  92. |     menuItem -> An integer showing which menu item was selected.        |
  93. |-----------------------------------------------------------------------|
  94. | Returns :: void.                                                        |
  95. \***********************************************************************/
  96.  
  97. static void Do_File_Cmd (menuItem)
  98. int menuItem;
  99. {
  100.     long    ticks;            
  101.     Handle    h;
  102.     ProcPtr    pp;
  103.     
  104.     switch (menuItem) {
  105.         case HelpItem:
  106.             /* Enter the code resource here! */
  107.             h = GetResource('HELP',128);
  108.             if((ResErr == noErr) && (h != NIL)) {
  109.                 HLock(h);
  110.                 pp = (ProcPtr) *h;
  111.                 (*pp)();
  112.                 HUnlock(h); 
  113.                 ReleaseResource(h);
  114.             }
  115.             else
  116.               SysBeep(1);
  117.             break;
  118.         case QuitItem:
  119.             program_done = TRUE;
  120.             Delay(8,&ticks);
  121.             FlashMenuBar(FileID);
  122.             break;
  123.     }
  124. }
  125.  
  126.